package com.alibaba.citrus.cr.mn.order.center.forecastorder.entity;

import com.alibaba.citrus.cr.common.constant.OrderFeaturesKey;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.mn.order.center.common.constants.ForecastConstants;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.ApplicationContextHelper;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.*;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.ability.ForecastOrderAbility;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.ability.NormalPriceQueryUtils;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.convert.ForecastOrderBOToSDOConvertor;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.db.service.CustomSequenceAccessor;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.alibaba.lattice2.epoch.user.service.EpochLoginUserService;
import com.epoch.app.bcorder.forecast_sales.ForecastOrderTag;
import com.epoch.app.bcorder.service.BcOrderService;
import com.epoch.app.bcorder.service.UnifiedInventoryService;
import com.epoch.app.crforecastsales.api.forecastorderlinequery.service.ForecastOrderLineQueryService;
import com.epoch.app.crforecastsales.api.forecastorderwrite.service.ForecastOrderWriteService;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.crforecastsales.bo.ForecastOrderLineBO;
import com.epoch.app.crforecastsales.domian.activitytitle.service.ActivityTitleService;
import com.epoch.app.crforecastsales.domian.activitytitleext.service.ActivityTitleExtService;
import com.epoch.app.crforecastsales.enums.ForecastStatusEnum;
import com.epoch.app.crforecastsales.model.enums.ForecastDealStatusSEO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.crplatformenhance.api.scitemquery.service.ScItemQueryService;
import com.epoch.app.crplatformenhance.api.strategyquery.service.StrategyQueryService;
import com.epoch.app.crplatformenhance.domain.scitemunit.service.ScItemUnitService;
import com.epoch.app.crplatformenhance.inventoryallotlinewrite.service.InventoryAllotLineWriteService;
import com.epoch.app.crplatformenhance.inventoryallotquery.service.InventoryAllotQueryService;
import com.epoch.app.crpricecenter.api.activityquery.service.ActivityQueryService;
import com.epoch.app.crpricecenter.api.activitywrite.service.ActivityWriteService;
import com.epoch.app.crunifiedinventory.api.allocateorderwrite.service.AllocateOrderWriteService;
import com.epoch.app.crunifiedinventory.service.CrUnifiedInventoryService;
import com.epoch.app.mnordercenter.model.dto.CreateForecastResponseDTO;
import com.google.common.collect.Lists;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

@Getter
@SuppressWarnings("unchecked")
public class ForecastOrderEntity {
    private static final Log log = Log.getLogger(ForecastOrderEntity.class);
    private ForecastOrderBO forecastOrderBO;

    private static final ActivityQueryService activityQueryService;
    private static final AllocateOrderWriteService allocateOrderWriteService;
    private static final StrategyQueryService strategyQueryService;
    private static final ActivityWriteService activityWriteService;
    private static final CustomSequenceAccessor customSequenceAccessor;
    private static final ScItemQueryService scItemQueryService;
    private static final ForecastOrderWriteService forecastOrderWriteService;
    private static final ScItemUnitService scItemUnitService;
    private static final NormalPriceQueryUtils priceQueryUtils;
    private static final UnifiedInventoryService unifiedInventoryService;
    private static final EpochLoginUserService epochLoginUserService;
    //处理队列写
    private static final DealLineWriteRepository dealLineWriteRepository;
    //处理队列读
    private static final DealLineQueryRepository dealLineQueryRepository;
    //主单读
    private static final ForecastOrderQueryRepository forecastOrderQueryRepository;
    //主单写
    private static final ForecastOrderWriteRepository forecastOrderWriteRepository;
    //子订单写
    private static final ForecastOrderLineWriteRepository forecastOrderLineWriteRepository;
    //子订单读
    private static final ForecastOrderLineQueryRepository forecastOrderLineQueryRepository;
    private static final InventoryAllotQueryService inventoryAllotQueryService;
    private static final InventoryAllotLineWriteService inventoryAllotLineWriteService;
    private static final BcOrderService bcOrderService;
    private static final ActivityTitleExtService activityTitleExtService;
    private static final ActivityTitleService activityTitleService;
    private static final CrUnifiedInventoryService crUnifiedInventoryService;
    private static final ForecastOrderLineQueryService forecastOrderLineQueryService;

    static {
        activityQueryService = SpringContextUtil.getBean(ActivityQueryService.class);
        allocateOrderWriteService = SpringContextUtil.getBean(AllocateOrderWriteService.class);
        strategyQueryService = SpringContextUtil.getBean(StrategyQueryService.class);
        activityWriteService = SpringContextUtil.getBean(ActivityWriteService.class);
        customSequenceAccessor = SpringContextUtil.getBean(CustomSequenceAccessor.class);
        scItemQueryService = SpringContextUtil.getBean(ScItemQueryService.class);
        forecastOrderWriteService = SpringContextUtil.getBean(ForecastOrderWriteService.class);
        scItemUnitService = SpringContextUtil.getBean(ScItemUnitService.class);
        priceQueryUtils = SpringContextUtil.getBean(NormalPriceQueryUtils.class);
        unifiedInventoryService = SpringContextUtil.getBean(UnifiedInventoryService.class);
        dealLineWriteRepository = SpringContextUtil.getBean(DealLineWriteRepository.class);
        forecastOrderQueryRepository = SpringContextUtil.getBean(ForecastOrderQueryRepository.class);
        dealLineQueryRepository = SpringContextUtil.getBean(DealLineQueryRepository.class);
        forecastOrderWriteRepository = SpringContextUtil.getBean(ForecastOrderWriteRepository.class);
        forecastOrderLineWriteRepository = SpringContextUtil.getBean(ForecastOrderLineWriteRepository.class);
        forecastOrderLineQueryRepository = SpringContextUtil.getBean(ForecastOrderLineQueryRepository.class);
        inventoryAllotQueryService = SpringContextUtil.getBean(InventoryAllotQueryService.class);
        inventoryAllotLineWriteService = SpringContextUtil.getBean(InventoryAllotLineWriteService.class);
        bcOrderService = SpringContextUtil.getBean(BcOrderService.class);
        epochLoginUserService = SpringContextUtil.getBean(EpochLoginUserService.class);
        activityTitleExtService = SpringContextUtil.getBean(ActivityTitleExtService.class);
        activityTitleService = SpringContextUtil.getBean(ActivityTitleService.class);
        crUnifiedInventoryService = SpringContextUtil.getBean(CrUnifiedInventoryService.class);
        forecastOrderLineQueryService = SpringContextUtil.getBean(ForecastOrderLineQueryService.class);

    }

    public static ForecastOrderEntity of(ForecastOrderBO forecastOrderBO) {
        ForecastOrderEntity forecastEntity = new ForecastOrderEntity();
        forecastEntity.forecastOrderBO = forecastOrderBO;
        return forecastEntity;
    }

    public ForecastOrderAbility getForecastOrderAbility() {
        return ApplicationContextHelper.getBean("forecastOrderAbility");
    }

    /**
     * 创建预报订单 失败需要回写返回
     */
    public String create() {
        // 保存预报主单
        ForecastOrderSDO forecastOrderSDO = ForecastOrderBOToSDOConvertor.convert(this.forecastOrderBO);
        String nextStringSequence = customSequenceAccessor.getNextStringSequence("cr_forecast_sales", "forecastOrderId");
        forecastOrderSDO.setOrderBizId(nextStringSequence);
        //设置为草稿状态
        forecastOrderSDO.setStatus(ForecastStatusEnum.DRAFT.getNumValue());
        //保存预报订单主单数据
        String orderId = forecastOrderWriteRepository.save(forecastOrderSDO);

        AtomicInteger num = new AtomicInteger(0);//行号
        this.forecastOrderBO.setId(orderId);
        this.forecastOrderBO.setOrderBizId(nextStringSequence);
        List<ForecastOrderLineBO> forecastOrderLineBOList = this.forecastOrderBO.getForecastOrderLineBOList();
        Map<String, ForecastOrderLineBO> lineBOMap = forecastOrderLineBOList.stream().collect(Collectors.toMap(ForecastOrderLineBO::getSourceLineNum, Function.identity()));

        for (ForecastOrderLineSDO forecastOrderLineSDO : forecastOrderSDO.getForecastOrderLineSDOList()) {
            if (forecastOrderLineSDO.getUnitPrice() == null || forecastOrderLineSDO.getUnitPrice() < 0L) {
                throw new FunctionException("OC-04-001-01-16-014");
            }
            forecastOrderLineSDO.setMainOrderBizId(nextStringSequence);
            forecastOrderLineSDO.setMainOrderId(orderId);
            forecastOrderLineSDO.setStatus(ForecastStatusEnum.DRAFT.getNumValue());
            //判断是不是新增的子单 老的子单更新 新的子单创建
            String lineId = saveForecastOrderLine(num, forecastOrderLineSDO);
            if (lineBOMap.containsKey(forecastOrderLineSDO.getSourceLineNum())) {
                ForecastOrderLineBO forecastOrderLineBO = lineBOMap.get(forecastOrderLineSDO.getSourceLineNum());
                forecastOrderLineBO.setId(Long.parseLong(lineId));
                forecastOrderLineBO.setLineNum(forecastOrderLineSDO.getSerialNumber());
            }
        }
        ArrayList<ForecastOrderLineBO> forecastOrderLineBOS = Lists.newArrayList(lineBOMap.values());
        this.forecastOrderBO.setForecastOrderLineBOList(forecastOrderLineBOS);
        return orderId;
    }

    /**
     * 创建预报订单 失败需要回写返回
     */
    public String modify(ForecastOrderSDO origin) {
        this.forecastOrderBO.setId(origin.getId());
        this.forecastOrderBO.setOrderBizId(origin.getOrderBizId());
        this.forecastOrderBO.setStatus(origin.getStatus());
        ForecastOrderSDO target = ForecastOrderBOToSDOConvertor.convert(this.forecastOrderBO);
        List<ForecastOrderLineSDO> originLineList = origin.getForecastOrderLineSDOList();
        List<ForecastOrderLineSDO> targetLineList = target.getForecastOrderLineSDOList();

        // 和原明细进行对比，存在相同行号的进行更新操作
        List<String> originLineNos = Nullable.stream(originLineList)
                .map(ForecastOrderLineSDO::getSerialNumber)
                .collect(Collectors.toList());
        List<ForecastOrderLineSDO> updateList = Nullable.stream(targetLineList)
                .filter(o -> originLineNos.contains(o.getSerialNumber()))
                .collect(Collectors.toList());
        log.info("updateList参数:{}",JSON.toJSONString(updateList));

        // 和原明细进行对比，不存在的行号进行新增操作
        List<ForecastOrderLineSDO> addList = Nullable.stream(targetLineList)
                .filter(o -> StringUtils.isBlank(o.getSerialNumber()))
                .collect(Collectors.toList());
        log.info("addList参数:{}",JSON.toJSONString(addList));

        // 和原明细进行对比，原行号不存在的进行删除操作
        List<String> targetLineNos = Nullable.stream(targetLineList)
                .map(ForecastOrderLineSDO::getSerialNumber)
                .filter(StringUtils::isNotBlank)
                .distinct().collect(Collectors.toList());
        List<ForecastOrderLineSDO> deleteList = Nullable.stream(originLineList)
                .filter(o -> !targetLineNos.contains(o.getSerialNumber()))
                .collect(Collectors.toList());
        log.info("deleteList参数:{}",JSON.toJSONString(deleteList));

        Map<String, ForecastOrderLineSDO> originLineMap = Nullable.stream(originLineList)
                .collect(Collectors.toMap(ForecastOrderLineSDO::getSerialNumber, Function.identity(), (v1, v2) -> v1));
        for (ForecastOrderLineSDO forecastOrderLineSDO : updateList) {
            ForecastOrderLineSDO originLine = originLineMap.get(forecastOrderLineSDO.getSerialNumber());
            forecastOrderLineSDO.setId(originLine.getId());
            forecastOrderLineSDO.setStatus(originLine.getStatus());
            forecastOrderLineSDO.setActualTotalReplyAmount(originLine.getActualTotalReplyAmount());
            // 修改明细行
            modifyForecastOrderLine(forecastOrderLineSDO);
        }

        if (!ForecastStatusEnum.COMPLETED.getNumValue().equals(target.getStatus())) {
            // 已完成状态不允许增加和删除明细，防止蒙维斯修改回告状态时误删数据
            for (ForecastOrderLineSDO forecastOrderLineSDO : deleteList) {
                // 删除明细行
                deleteForecastOrderLine(forecastOrderLineSDO);
            }

            AtomicInteger num = new AtomicInteger(0);
            // 获取当前明细行最大的行号，然后往上累加
            num.set(updateList.stream().map(o -> Integer.parseInt(o.getSerialNumber())).max(Comparator.comparingInt(v -> v)).orElse(0) / 10);
            for (ForecastOrderLineSDO forecastOrderLineSDO : addList) {
                // 新增明细行
                saveForecastOrderLine(num, forecastOrderLineSDO);
            }
        }

        // 推进主单状态
        if (ForecastStatusEnum.PARTIAL_REPLY.getNumValue().equals(target.getStatus())) {
            updateList.addAll(addList);
            boolean isCompleted = updateList.stream().anyMatch(line -> ForecastStatusEnum.WAITING_FOR_REPLY.getNumValue().equals(line.getStatus())
                    || ForecastStatusEnum.PARTIAL_REPLY.getNumValue().equals(line.getStatus()));
            if (!isCompleted) {
                target.setStatus(ForecastStatusEnum.COMPLETED.getNumValue());
            }
        }

        // 修改主单
        forecastOrderWriteRepository.modify(target);
        return origin.getId();
    }

    private void deleteForecastOrderLine(ForecastOrderLineSDO forecastOrderLineSDO) {
        forecastOrderLineWriteRepository.remove(forecastOrderLineSDO);
    }

    private String saveForecastOrderLine(AtomicInteger num, ForecastOrderLineSDO forecastOrderLineSDO) {
        ForecastOrderSDO forecastOrderSDOConvent = ForecastOrderBOToSDOConvertor.convert(this.forecastOrderBO);
        //创建子单
        ForecastOrderSDO forecastOrderSDO = forecastOrderLineQueryRepository.queryByOrderId(forecastOrderSDOConvent);

        //设置行号
        num.getAndAdd(1);
        int serialNumber = num.get() * 10;
        forecastOrderLineSDO.setSerialNumber(Integer.toString(serialNumber));
        //设置位草稿状态
        if (Objects.nonNull(forecastOrderSDO.getWareHouseCode())) {
            forecastOrderLineSDO.setWarehouseArea(forecastOrderSDO.getWareHouseCode());
        }
        forecastOrderLineSDO.setCustomerCode(forecastOrderSDO.getCustomerCode());
        if (Objects.nonNull(forecastOrderSDO.getStatus())) {
            // 主单为"部分回复"时，设置子单为"待回复"
            if(ForecastStatusEnum.PARTIAL_REPLY.getNumValue().equals(forecastOrderSDO.getStatus())){
                forecastOrderLineSDO.setStatus(ForecastStatusEnum.WAITING_FOR_REPLY.getNumValue());
            }else {
                forecastOrderLineSDO.setStatus(forecastOrderSDO.getStatus());
            }
        }
        forecastOrderLineSDO.setDealStatus(ForecastDealStatusSEO.WAITING_DEAL.getStatus());
        forecastOrderLineSDO.setMainOrderId(forecastOrderSDO.getId());

        forecastOrderLineSDO.setMainOrderBizId(forecastOrderSDO.getOrderBizId());
        forecastOrderLineSDO.setModifierName(forecastOrderSDO.getModifierName());
        forecastOrderLineSDO.setModofierCode(forecastOrderSDO.getModifierCode());
        forecastOrderLineSDO.setModifierId(forecastOrderSDO.getModifierId());
        forecastOrderLineSDO.setCreatorId(forecastOrderSDO.getCreatorId());
        forecastOrderLineSDO.setCreatorCode(forecastOrderSDO.getCreatorCode());
        forecastOrderLineSDO.setCreatorName(forecastOrderSDO.getCreatorName());
        //默认累计回复数量为0
        forecastOrderLineSDO.setActualTotalReplyAmount(null);
        //仓库信息
        // 一盘货改造，每行的发货仓可以不一样
//        forecastOrderLineSDO.setShippingWarehouseCode(forecastOrderSDO.getShippingWarehouseCode());
//        forecastOrderLineSDO.setShippingWarehouseId(forecastOrderSDO.getShippingWarehouseId());
//        forecastOrderLineSDO.setShippingWarehouseName(forecastOrderSDO.getShippingWarehouseName());
//        forecastOrderLineSDO.setStockCode(forecastOrderSDO.getStockCode());
//        forecastOrderLineSDO.setDeliveryFactoryCode(forecastOrderSDO.getDeliveryFactoryCode());

        //设置总金额
        if (StringUtils.isNotEmpty(forecastOrderLineSDO.getQuantity()) && Objects.nonNull(forecastOrderLineSDO.getUnitPrice())) {
            forecastOrderLineSDO.setTotalFee(Long.parseLong(forecastOrderLineSDO.getQuantity()) * forecastOrderLineSDO.getUnitPrice());
        }
        HashMap<String, Object> stringObjectHashMap = MapUtils.isEmpty(forecastOrderLineSDO.getFeatures()) ? new HashMap<>() : (HashMap<String, Object>) forecastOrderLineSDO.getFeatures();
        if (CollectionUtils.isNotEmpty(forecastOrderLineSDO.getSaleUnitList())) {
            stringObjectHashMap.put(ForecastConstants.SALE_UNIT_LIST, forecastOrderLineSDO.getSaleUnitList());
        }
        stringObjectHashMap.put(ForecastConstants.OS, ForecastConstants.ORDER_CENTER);
        forecastOrderLineSDO.setFeatures(stringObjectHashMap);
        String lineId = forecastOrderLineWriteRepository.save(forecastOrderLineSDO);
        forecastOrderLineSDO.setId(Long.parseLong(lineId));
        return lineId;
    }

    private void modifyForecastOrderLine(ForecastOrderLineSDO forecastOrderLineSDO) {
        forecastOrderLineSDO.setModifierId(this.forecastOrderBO.getModifierId());
        forecastOrderLineSDO.setModofierCode(this.forecastOrderBO.getModifierCode());
        forecastOrderLineSDO.setModifierName(this.forecastOrderBO.getModifierName());
        forecastOrderLineSDO.setGmtModified(new Date());
        //设置总金额
        if (StringUtils.isNotEmpty(forecastOrderLineSDO.getQuantity()) && Objects.nonNull(forecastOrderLineSDO.getUnitPrice())) {
            forecastOrderLineSDO.setTotalFee(Long.parseLong(forecastOrderLineSDO.getQuantity()) * forecastOrderLineSDO.getUnitPrice());
        }
        ForecastOrderLineSDO orderLineSDO = getOrderLineFeatures(forecastOrderLineSDO);
        forecastOrderLineWriteRepository.modify(orderLineSDO);
    }

    /**
     * 预报订单提交
     */
    public Result<Boolean> submit(String modifierId, String modifierCode, String modifierName) {

        ForecastOrderSDO forecastOrderSDO = ForecastOrderBOToSDOConvertor.convert(this.forecastOrderBO);
        if (CollectionUtils.isEmpty(forecastOrderSDO.getForecastOrderLineSDOList())) {
            throw new FunctionException("OC-01-001-01-15-001", "预报订单明细");
        }
        forecastOrderSDO.setStatus(ForecastStatusEnum.WAITING_FOR_REPLY.getNumValue());
        forecastOrderSDO.setModifierId(modifierId);
        forecastOrderSDO.setModifierCode(modifierCode);
        forecastOrderSDO.setModifierName(modifierName);

        // 提交子单
        for (ForecastOrderLineSDO forecastOrderLineSDO : forecastOrderSDO.getForecastOrderLineSDOList()) {
            //add by myw,补货订单没有选中活动ID和活动项目号不允许提交,20221128
            if (ForecastOrderTag.RESTOCK_FORECAST.getValue().equals(forecastOrderSDO.getOrderType())) {
                if (StringUtils.isBlank(forecastOrderLineSDO.getActivityCode()) || StringUtils.isBlank(forecastOrderLineSDO.getActivityNumber())) {
                    throw new FunctionException("OC-01-001-01-15-001", "活动编码、活动项目号");
                }
            }

            if (StringUtils.isEmpty(forecastOrderLineSDO.getQuantity())) {
                throw new FunctionException("OC-01-001-01-15-001", "预报数量");
            }

            forecastOrderLineSDO.setStatus(ForecastStatusEnum.WAITING_FOR_REPLY.getNumValue());
            forecastOrderLineSDO.setModifierId(modifierId);
            forecastOrderLineSDO.setModofierCode(modifierCode);
            forecastOrderLineSDO.setModifierName(modifierName);

            forecastOrderLineWriteRepository.submit(forecastOrderLineSDO);

        }
        //提交主单
        forecastOrderWriteRepository.submit(forecastOrderSDO);
//        LogUtils.printOperateLog(this.forecastOrderSDO.getId(), ForecastOrderLogEnum.SUBMIT.getForecastOrderLogValue(), "提交预报订单", loginUser);

        return Result.success(true, "预报订单有效性检查通过，耐心等待调度员当日回复");
    }

    public ForecastOrderLineSDO getOrderLineFeatures(ForecastOrderLineSDO forecastOrderLineSDO) {
        List<String> ids = new ArrayList<>(1);
        ids.add(String.valueOf(forecastOrderLineSDO.getId()));
        Map newLineFeature = forecastOrderLineSDO.getFeatures();
        PageResult<List<ForecastOrderLineSDO>> result = forecastOrderLineQueryRepository.batchLoadList(ids, null);
        if (CollectionUtils.isEmpty(result.getContent())) {
            return forecastOrderLineSDO;
        }
        Map<String, Object> features = result.getContent().get(0).getFeatures();
        if (MapUtils.isEmpty(features)) {
            features = new HashMap<>();
        }

        if (StringUtils.isNotBlank(forecastOrderLineSDO.getActivityPriceRemark())) {
            features.put(ForecastConstants.ACTIVITY_PRICE_REMARK, forecastOrderLineSDO.getActivityPriceRemark());
        }
        if (CollectionUtils.isNotEmpty(forecastOrderLineSDO.getSaleUnitList())) {
            features.put(ForecastConstants.SALE_UNIT_LIST, forecastOrderLineSDO.getSaleUnitList());
        }
        if (MapUtils.isNotEmpty(newLineFeature) && newLineFeature.containsKey(ForecastConstants.ACTIVITY_NUMBER)) {
            features.put(ForecastConstants.ACTIVITY_NUMBER, newLineFeature.get(ForecastConstants.ACTIVITY_NUMBER));
        }
        // 一盘货新增字段
        if (Objects.nonNull(forecastOrderLineSDO.getSourceUnit())) {
            features.put(OrderFeaturesKey.SOURCE_UNIT, forecastOrderLineSDO.getSourceUnit());
        }
        if (Objects.nonNull(forecastOrderLineSDO.getSourceQuantity())) {
            features.put(OrderFeaturesKey.SOURCE_QUANTITY, forecastOrderLineSDO.getSourceQuantity());
        }
        if (Objects.nonNull(forecastOrderLineSDO.getSourcePrice())) {
            features.put(OrderFeaturesKey.SOURCE_PRICE, forecastOrderLineSDO.getSourcePrice());
        }
        String bizFeatures = forecastOrderLineSDO.getBizFeatures();
        if (StringUtils.isNotBlank(bizFeatures)) {
            JSONObject bizFeaturesJson = JSONObject.parseObject(bizFeatures);
            Object deadline = bizFeaturesJson.get(OrderFeaturesKey.DEADLINE);
            if (Objects.nonNull(deadline)) {
                features.put(OrderFeaturesKey.DEADLINE, DateUtils.toMillisecondByTime((String) deadline));
            }
        }
        forecastOrderLineSDO.setFeatures(features);
        log.info("编辑子单信息: {}", JSONObject.toJSONString(forecastOrderLineSDO));
        return forecastOrderLineSDO;
    }

    public CreateForecastResponseDTO buildLostLockCreateResponse(String taskId) {
        CreateForecastResponseDTO createForecastResponseDTO = new CreateForecastResponseDTO();
        createForecastResponseDTO.setBatchTaskId(taskId);
        createForecastResponseDTO.setReqNo(forecastOrderBO.getReqNo());
        createForecastResponseDTO.setSuccess(Boolean.FALSE);
        createForecastResponseDTO.setMessage("分布式锁获取失败");
        createForecastResponseDTO.setErrorCode("OC-00-000-01-99-002");
        createForecastResponseDTO.setSourceOrderNo(forecastOrderBO.getSourceOrderNo());
        return createForecastResponseDTO;
    }
}
